//
//  ScanGoodErrorAlertView.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/11/11.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "ScanGoodErrorAlertView.h"

@interface ScanGoodErrorAlertView ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *infoBGView;
@property (nonatomic, strong) UILabel *contentLab;
@property (nonatomic, strong) UILabel *line;
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, strong) UILabel *title1Lab;
@property (nonatomic, strong) UILabel *content1Lab;
@property (nonatomic, strong) UILabel *title2Lab;
@property (nonatomic, strong) UILabel *content2Lab;

@end

@implementation ScanGoodErrorAlertView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.frame = CGRectMake(0, 0, ScreenW, ScreenH);
        self.backgroundColor = HexColorA(0x000000, 0.5);
        
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(30, (ScreenH - 250) * 0.5, ScreenW - 60, 250)];
        self.bgView.backgroundColor = HexColor(0xffffff);
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 6;
        [self addSubview:self.bgView];
        
        self.infoBGView = [[UIView alloc] initWithFrame:CGRectMake(15, 15, self.bgView.nim_width - 30, 80)];
        self.infoBGView.layer.masksToBounds = YES;
        self.infoBGView.layer.cornerRadius = 2;
        self.infoBGView.backgroundColor = HexColor(0xF7F7FA);
        [self.bgView addSubview:self.infoBGView];
        
        self.title1Lab = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, [FactoryMethod sizeWithText:@"货物名称：" font:BoldFontOfSize(16) maxSize:CGSizeMake(0, 0)].width, 17)];
        self.title1Lab.text = @"货物名称：";
        self.title1Lab.font = BoldFontOfSize(16);
        self.title1Lab.textColor = HexColor(0x9C9DB3);
        [self.infoBGView addSubview:self.title1Lab];
        
        self.content1Lab = [[UILabel alloc] initWithFrame:CGRectMake(self.title1Lab.nim_right, self.title1Lab.nim_top, self.infoBGView.nim_width - 20 - self.title1Lab.nim_right, self.title1Lab.nim_height)];
        self.content1Lab.textColor = HexColor(0x2B2C44);
        self.content1Lab.font = BoldFontOfSize(16);
        [self.infoBGView addSubview:self.content1Lab];
        
        self.title2Lab = [[UILabel alloc] initWithFrame:CGRectMake(20, self.title1Lab.nim_bottom + 5, self.title1Lab.nim_width, 17)];
        self.title2Lab.font = BoldFontOfSize(16);
        self.title2Lab.textColor = HexColor(0x9C9DB3);
        [self.infoBGView addSubview:self.title2Lab];
        
        self.content2Lab = [[UILabel alloc] initWithFrame:CGRectMake(self.title2Lab.nim_right, self.title2Lab.nim_top, self.infoBGView.nim_width - 20 - self.title2Lab.nim_right, self.title2Lab.nim_height)];
        self.content2Lab.textColor = HexColor(0x2B2C44);
        self.content2Lab.font = BoldFontOfSize(16);
        [self.infoBGView addSubview:self.content2Lab];
        
        self.closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.bgView.nim_height - 50, self.bgView.nim_width, 50)];
        [self.closeBtn setTitle:@"关闭" forState:0];
        [self.closeBtn setTitleColor:Color_ThemeColor forState:0];
        self.closeBtn.titleLabel.font = BoldFontOfSize(16);
        [self.closeBtn addTarget:self action:@selector(disMissView) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:self.closeBtn];
        
        self.line = [[UILabel alloc] initWithFrame:CGRectMake(0, self.closeBtn.nim_top - 1, self.bgView.nim_width, 1)];
        self.line.backgroundColor = Color_backgroundColor;
        [self.bgView addSubview:self.line];
        
        self.contentLab = [UILabel new];
        self.contentLab.textColor = HexColor(0x9C9DB3);
        self.contentLab.font = [UIFont systemFontOfSize:14];
        self.contentLab.numberOfLines = 0;
        [self.bgView addSubview:self.contentLab];
    }
    return self;
}

-(void)setType:(ScanGoodErrorType)type {
    _type = type;
    if (type == ScanGoodErrorTypeGoodError) {
        self.contentLab.text = @"当前扫码的货物型号与运单中货物型号不符，请取消或重新扫码！";
        self.title2Lab.text = @"货物型号：";
    }else if (type == ScanGoodErrorTypeNoGood) {
        self.contentLab.text = @"当前运单中没有此货物，请取消或重新扫描！";
        self.title2Lab.text = @"货物型号：";
    }else if (type == ScanGoodErrorTypeBoxError) {
        self.contentLab.text = @"此箱已被绑过，请取消或重新扫描！";
        self.title2Lab.text = @"货物型号：";
    }else if (type == ScanGoodErrorTypeCountError) {
        self.contentLab.text = @"货物件数已超过运单中件数，请取消或删除已绑定的包装，重新扫码。";
        self.title2Lab.text = @"运单件数：";
    }
    self.contentLab.nim_left = 15;
    self.contentLab.nim_width = self.bgView.nim_width - 30;
    [self.contentLab sizeToFit];
    self.contentLab.nim_width = self.bgView.nim_width - 30;
    self.contentLab.nim_top = self.infoBGView.nim_bottom + (self.line.nim_top - self.infoBGView.nim_bottom - self.contentLab.nim_height) * 0.5;
}

-(void)setContent1:(NSString *)content1 {
    _content1 = content1;
    self.content1Lab.text = content1;
}

-(void)setContent2:(NSString *)content2 {
    _content2 = content2;
    self.content2Lab.text = content2;
}

- (void)showInView:(UIView *)view {
    if (!view) {
        return;
    }
    [view addSubview:self];
}

- (void)disMissView {
    [self removeFromSuperview];
}

@end
